package com.tms.springboot.controller;


import com.github.pagehelper.PageInfo;
import com.tms.springboot.base.pojo.PdTransportLine;
import com.tms.springboot.result.JsonResult;
import com.tms.springboot.result.ResultTool;
import com.tms.springboot.service.PdTransportLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 线路表 前端控制器
 * </p>
 *
 * @author cjj
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-transport-line")
public class PdTransportLineController {

    @Autowired
    private PdTransportLineService transportLineService;

    /**
     * todo 添加线路 transportLineType
     *
     * @param transportLine 线路类型数据对象
     * @return JsonResult
     */
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody PdTransportLine transportLine) {
        return transportLineService.save(transportLine) ? ResultTool.success() : ResultTool.fail();
    }

    /**
     * todo 修改线路 transportLineType
     *
     * @param transportLine 线路类型数据对象
     * @return JsonResult
     */
    @PostMapping("/updateById")
    public JsonResult updateById(@RequestBody PdTransportLine transportLine) {
        return transportLineService.updateById(transportLine) ? ResultTool.success() : ResultTool.fail();
    }

    /**
     * todo 根据id删除线路数据 transportLineType
     *
     * @param id 需要删除的恶线路类型的id
     * @return JsonResult
     */
    @GetMapping("/deleteById/{id}")
    public JsonResult deleteById(@PathVariable Integer id) {
        return transportLineService.removeById(id) ? ResultTool.success() : ResultTool.fail();
    }

    @GetMapping("/queryAll")
    public JsonResult queryAll() {

        List<PdTransportLine> transportLines = transportLineService.list();

        return transportLines != null ? ResultTool.success(transportLines) : ResultTool.fail();
    }

    /**
     * todo 根据条件查询线路，分页
     *
     * @param currentPage   当前页码
     * @param pageSize      每页显示条数
     * @param transportLine 查询条件
     * @return JsonResult
     */
    @PostMapping("/queryByConditionAndPage/{currentPage}/{pageSize}")
    public JsonResult queryByConditionAndPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, @RequestBody PdTransportLine transportLine) {

        PageInfo<PdTransportLine> transportLineTypePageInfo = transportLineService.queryByConditionAndPage(currentPage, pageSize, transportLine);

        if (transportLineTypePageInfo.getList() != null) {
            return ResultTool.success(transportLineTypePageInfo);
        } else {
            return ResultTool.fail();
        }

    }
}
